﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01MatricesNbyN
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter value for n:");
            string strN = Console.ReadLine();
            int n = int.Parse(strN);
            int value = 1;
            int[,] matrix = new int[n,n];
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    matrix[i, j] = value;
                    value++;
                }
            }
            // Printing:
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Console.Write("{0,2} ", matrix[i, j]);
                }
                Console.WriteLine();
            }
            //Case a:**********************************************************************************
            Console.WriteLine("---------------------------------------");
            Console.WriteLine("Case a:");
            value = 1;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    matrix[j, i] = value;
                    value++;
                }
            }
            // Printing:
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Console.Write("{0,2}  ", matrix[i, j]);
                }
                Console.WriteLine();
            }
            //Case b:**********************************************************************************
            Console.WriteLine("---------------------------------------");
            Console.WriteLine("Case b:");
            value = 1;
            int option = 0;
            for (int i = 0; i < n; i++)
            {
                if (option % 2 != 0)
                {
                    for (int j = n - 1; j >= 0; j--)
                    {
                        matrix[j, i] = value;
                        value++;
                    }
                }
                if (option % 2 == 0)
                {
                        for (int j = 0; j < n; j++)
                    {
                        matrix[j, i] = value;
                        value++;
                    }
                }
                option++;
                
            }
            // Printing:
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Console.Write("{0,2}  ", matrix[i, j]);
                }
                Console.WriteLine();
            }
            // Case c:**********************************************************************************
            Console.WriteLine("---------------------------------------");
            Console.WriteLine("Case c:");
            value = 1;
            option = 0;
            for (int i = n - 1; i >= 0; i--)
            {
                for (int j = 0; j < n - i; j++)
                {
                    matrix[i + j, j] = value;
                    value++;
                }
            }
            for (int i = 1; i < n; i++)
            {
                for (int j = 0; j < n - i; j++)
                {
                    matrix[j, i + j] = value;
                    value++;
                }
            }
            // Printing:
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Console.Write("{0,2} ", matrix[i, j]);
                }
                Console.WriteLine();
            }
            // Case d:**********************************************************************************
            // This only works good until n=4, have to come back and think about it again later.
            Console.WriteLine("---------------------------------------");
            Console.WriteLine("Case d:");
            value = 1;
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                matrix[i, n-n] = value;
                value++;
            }
            for (int i = 1; i < n; i++)
            {
                matrix[n - 1, i] = value;
                value++;
            }
            for (int i = n-2; i >=0; i--)
            {
                matrix[i, n - 1] = value;
                value++;
            }
            for (int i = n-2; i >= 1 ; i--)
            {
                matrix[n - n, i] = value;
                value++;
            }
            for (int i = 1; i < n - 1; i++)
            {
                matrix[i, n - 3] = value;
                value++;
            }
            for (int i = n-2; i >= n-3; i--)
            {
                matrix[i, n - 2] = value;
                value++;
            }
            // Printing:
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Console.Write("{0,2} ", matrix[i, j]);
                }
                Console.WriteLine();
            }
        }
    }
}
